/**
 * 
 */
package com.iwords.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author       BOLIN.HBC 
 * @time         2011-3-13    
 * @description  Code-tools       
 */
public class CodeUtils {
	
	private static final String DEFAULT_CODE = "utf-8";
	private static final Log log = LogFactory.getLog(CodeUtils.class);

	public static String encode(String value) {
		if (value == null) {
			return null;
		}
		if (value.length() == 0) {
			return "";
		}
		try {
			return URLEncoder.encode(value, DEFAULT_CODE);
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage(), e);
		}
		return null;

	}

	public static String decode(String value) {
		if (value == null || value.length() == 0) {
			return null;
		}

		try {
			return URLDecoder.decode(value, DEFAULT_CODE);
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage(), e);
		}
		return null;

	}
}
